//
// CShoutcastPlugin.cpp
// Your copyright notice
//

#include "ShoutcastPlugin.h"
#include "ShoutcastPlugin.rsg"
#include "ShoutcastUI.h"
#include "ShoutcastPlayer.h"
#include "PluginUtility.h"

#include <aknquerydialog.h>			//CAknTextQueryDialog

// Identifying string for plugin
_LIT(KShoutcastPlugin, "Shoutcast");

enum TPlayerPluginCommandId
{
	EPluginCommandId = 0x6200,
	EEnterShoutcastCommandId
};

// Construction and destruction functions
CShoutcastPlugin* CShoutcastPlugin::NewL(TAny* aInitParams)
{
	CShoutcastPlugin* self = new(ELeave) CShoutcastPlugin(aInitParams);
	CleanupStack::PushL(self);
	self->ConstructL(); 
	CleanupStack::Pop();

	return self;
}

CShoutcastPlugin::~CShoutcastPlugin()
{
	//Delete objects
	DELETE(iShoutcastPlayer);
	DELETE(iShoutcastUI);
	DELETE(iUrl);
	DELETE(iMetadata);

	//Delete resource file
	CCoeEnv::Static()->DeleteResourceFile(iOffset);
}

CShoutcastPlugin::CShoutcastPlugin(TAny* aInitParams)
:	iInitParams((CPluginInterface::TPluginInterfaceInitParams*)aInitParams), 
	iShoutcastPlayer(NULL),	iUrl(NULL), iMetadata(NULL), iShoutcastUI(NULL), iState(EStateNotReady)
{
}

void CShoutcastPlugin::ConstructL()
// Safely complete the initialization of the constructed object	
{
	//Create shoutcast player
	iShoutcastPlayer = CShoutcastPlayer::NewL(*this);

	//Create ui manager
	iShoutcastUI = CShoutcastUI::NewL();

	//Add rsc
	AddResourceFileL();
}

void CShoutcastPlugin::AddResourceFileL()
{
	TFileName f;

	//Get rsc filename
#if __WINS__
	f.Append(_L("Z:\\system\\libs\\plugins\\ShoutcastPlugin.rsc"));

#else
	Dll::FileName(f);

	//Change dll by rsc
	f.Replace(f.Find(_L(".dll")), 4, _L(".rsc"));
#endif

	//Read resource compiled file 
	iOffset = CEikonEnv::Static()->AddResourceFileL(f);
}

const TDesC& CShoutcastPlugin::GetMenuText()
{
	return KShoutcastPlugin;
}

TInt CShoutcastPlugin::GetMenuCommand()
{
	return EPluginCommandId;
}

void CShoutcastPlugin::AddExtraMenu(CEikMenuPane* aMenuPane)
{
	//Select shoutcast url
	CPluginUtility::AddMenuItem(EEnterShoutcastCommandId, _L("Open url..."), aMenuPane);
}

void CShoutcastPlugin::HandleCommandL(TInt aCommand)
{
	switch (aCommand)
	{
	case EEnterShoutcastCommandId:
		OpenUrlL();
		break;
	}
}

void CShoutcastPlugin::Draw(CWindowGc& aGc, const TRect& aRect) const
{
	//Draw background
	iShoutcastUI->Draw(aGc, aRect);
}

void CShoutcastPlugin::SetContainer(MPluginContainer* aContainer)
{
	CPluginInterface::SetContainer(aContainer);
	iShoutcastUI->SetContainer(aContainer);
}

void CShoutcastPlugin::Activate()
{
	//Activate ui
	iShoutcastUI->Activate();

	//Update metadata
	if (iMetadata) iShoutcastUI->SetTickerText(iMetadata->Des());
}

TRgb CShoutcastPlugin::BackBorderColor() const { return iShoutcastUI->BackBorderColor(); }
TRgb CShoutcastPlugin::BackShadowColor() const { return iShoutcastUI->BackShadowColor(); }
TRgb CShoutcastPlugin::BackColor() const { return iShoutcastUI->BackColor(); }
TRgb CShoutcastPlugin::InsideBorderColor() const { return iShoutcastUI->InsideBorderColor(); }
TRgb CShoutcastPlugin::InsideColor() const { return iShoutcastUI->InsideColor(); }
TRgb CShoutcastPlugin::ForeColor() const { return iShoutcastUI->ForeColor(); }

void CShoutcastPlugin::Deactivate()
{
	//Stop playing
	if (IsReadyToStop()) StopL();

	//Deactivate ui
	iShoutcastUI->Deactivate();
}

void CShoutcastPlugin::OpenUrlL()
{
	const TInt KMaxUrlLength = 255;
	TBuf<KMaxUrlLength> url;
	url.Append(_L("http://scfire-chi-aa01.stream.aol.com:80/stream/1074"));

	//Show dialog
	CAknTextQueryDialog* shoutcastQuery = CAknTextQueryDialog::NewL(url);
	CleanupStack::PushL(shoutcastQuery);
	shoutcastQuery->SetPromptL(_L("Enter shoutcast url:"));
	CleanupStack::Pop(shoutcastQuery);

	if (shoutcastQuery->ExecuteLD(R_DATAQUERY_DATA_QUERY))
	{
		//Stop before streaming
		if (IsReadyToStop()) StopL();

		//Start streaming
		StartStreamingL(url);
	}
}

void CShoutcastPlugin::StartStreamingL(const TDesC& aUri)
{
	//Copy url
	DELETE(iUrl);
	iUrl = aUri.AllocL();

	//Start playing streaming
	PlayL();
}

void CShoutcastPlugin::PlayL()
{
	//Set text and icon
	BufferStateChanged(ETrue, 0);

	//Streaming over url
	iShoutcastPlayer->StartL(iUrl->Des());

	iState = EStatePlaying;
}

void CShoutcastPlugin::StopL()
{
	//Stop player
	iShoutcastPlayer->StopL();

	//Set is buffering
	iShoutcastUI->SetIsBuffering(EFalse, -1);

	//Set icon
	iShoutcastUI->SetTickerIcon(CShoutcastUI::EStop);

	iState = EStateReadyToPlay;
}

void CShoutcastPlugin::PauseL()
{
}

TBool CShoutcastPlugin::IsReadyToPlay()
{
	return (iState == EStateReadyToPlay);
}

TBool CShoutcastPlugin::IsReadyToStop()
{
	return (iState == EStatePlaying);
}

TInt CShoutcastPlugin::Volume()
{
	return iShoutcastPlayer->Volume();
}

void CShoutcastPlugin::SetVolume(TInt aVolume)
{
	iShoutcastPlayer->SetVolume(aVolume);
}

void CShoutcastPlugin::BufferStateChanged(TBool aIsBuffering, TInt aPercent)
{
	iShoutcastUI->SetIsBuffering(aIsBuffering, aPercent);
}

void CShoutcastPlugin::Finished()
{
}

void CShoutcastPlugin::OnServerNameChanged(const TDesC& aServerName)
{
	iShoutcastUI->SetTitle(aServerName);
}

void CShoutcastPlugin::OnMetadataChanged(const TDesC& aMetadata)
{
	DELETE(iMetadata);
	iMetadata = aMetadata.Alloc();
	iShoutcastUI->SetTickerText(aMetadata);
}